package aml.bs.commons;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Title: StreamUtil
 * @Author duxuqiang02
 * @Date 2023/9/17 12:48
 * @description: Stream流工具类
 */
public class StreamUtil {

    /**
     * List 转化为Map Key自定义 value自定义
     * @param collection
     * @param function
     * @param tvFunction
     * @return
     * @param <T>
     * @param <K>
     * @param <V>
     */
    public static<T,K,V> Map<K,V> ListToMap(Collection<T> collection, Function<T,K> function,Function<T,V> tvFunction){
        if (collection.isEmpty()){
            return Collections.emptyMap();
        }
        return collection.stream().collect(Collectors.toMap(function,tvFunction,(a,b)->a));
    }

    /**
     * List 转化为Map Key自定义 value为T类型
     * @param collection
     * @param function
     * @return
     * @param <T>
     * @param <K>
     */
    public static<T,K> Map<K,T> ListToMapWithSingleKey(Collection<T> collection, Function<T,K> function){
        if (collection.isEmpty()){
            return Collections.emptyMap();
        }
        return collection.stream().collect(Collectors.toMap(function,Function.identity(),(a,b)->a));
    }
}
